// 03admin/12bizAuth/certAuth/quickAuth.js

var co = require('../../../../resource/js/common.js')
var app = getApp()
var _this, _data,_opt

Page({
   data: {
      //是否已经认证
      isAlreadyAuth: '',
      //是否成功请求认证
      isReqAuth: 0,
      //是否显示确认按钮
      isShowConfirm: 0,
      errMsg: '',
      tipImg: '',
      authAllInfo: {},
      authInfo: {}
   },

   onLoad: function(options) {
      _this = this
      _data = this.data
      _opt = options

      // _this.setData({
      //    isAlreadyAuth: app.Data.shopInfo.Is_Auth
      // })

      //已认证
      if (_data.isAlreadyAuth) {
         co.getShareApi('biz/Bizmg/getAuthInfo', {
            Biz_ID: _opt.Biz_ID
         }, function(res) {
            var obj = res.data
            obj.ApprovedTime = co.getFormatDate(1, obj.ApprovedTime)
            obj.EstiblishTime = co.getFormatDate(1, obj.EstiblishTime)
            _this.setData({
               authInfo: obj
            })
         })
      }
   },

   //提交认证
   authSubmit: function(e) {
      console.log(e);
      _this.setData({
         errMsg: ''
      })
      //参数
      var param = {
         Biz_ID: app.Data.shopInfo.Biz_ID,
         Company_Name: e.detail.value.Company_Name
      }
      wx.showLoading({
         title: '正在认证中...',
         mask: true,
      })
      //营业执照认证
      co.getShareApi('api/Company/authenticate', param, function(res) {
         console.log('authSubmit==', res);
         wx.hideLoading();
         if (res.code === 0) {
            //获取营业执照信息
            var obj = res.data.baseInfo

            //判断营业执照是否是餐饮业的
            if (obj.Name.indexOf('餐') < 0 && obj.Name.indexOf('饮') < 0 &&
               obj.BusinessScope.indexOf('餐') < 0 && obj.BusinessScope.indexOf('餐') < 0) {
               //余额扣款
               _this.balancePay()
               //显示错误图标   
               _this.setData({
                  isReqAuth: 1,
                  authAllInfo: res.data,
                  authInfo: obj
                  //errMsg: '营业执照不是餐饮业的营业执照',
                  //tipImg: 'http://h5.52g3.cc/icon/error.png'
               })
               return
            }

            obj.ApprovedTime = co.getFormatDate(1, obj.ApprovedTime)
            obj.EstiblishTime = co.getFormatDate(1, obj.EstiblishTime)
            _this.setData({
               isReqAuth: 1,
               isShowConfirm: 1,
               authAllInfo: res.data,
               authInfo: obj
            })

            // _this.setData({
            //    tipImg: 'http://h5.52g3.cc/icon/auth_ok.jpg'
            // })
         } else {
            //余额不足
            if (res.code == 29340) {
               //提示
               co.confirm('', res.msg, '取消', '确定', function(res) {
                  if (res) {
                     //跳转到充值页面
                     co.navigateTo('/03admin/09getMoney/05recharge/recharge', 0)
                  }
               })
            } else {
               //排除该营业执照已被其它商家认证(29311),就不做扣款处理
               if (res.code != 29311) {
                  //余额扣款
                  _this.balancePay()
               }
               //显示错误图标
               _this.setData({
                  isReqAuth: 0,
                  errMsg: res.msg,
                  tipImg: 'http://h5.52g3.cc/icon/error.png'
               })
            }
         }
      })
   },

   //确认认证信息
   confirmInfo: function(e) {
      _this.saveAuth(_data.authAllInfo)
   },

   //重新认证信息
   resetInfo: function(e) {
      app.Data.shopInfo.Is_Auth = 0;
      _this.setData({
         isAlreadyAuth: 0,
         isReqAuth: 0,
         isShowConfirm: 0,
         isErr: 0
      })
   },

   //保存认证信息
   saveAuth: function(data) {
      var param = data.baseInfo
      console.log('param==', param);

      param.Biz_ID = app.Data.shopInfo.Biz_ID

      // param.investorList = JSON.stringify(data.investorList)
      // param.investList = JSON.stringify(data.investList)
      // param.staffList = JSON.stringify(data.staffList)
      // param.branchList = JSON.stringify(data.branchList)

      param.investorList = data.investorList
      param.investList = data.investList
      param.staffList = data.staffList
      param.branchList = data.branchList
      param.FromTime = co.getFormatDate(1, data.FromTime)
      param.Auditor = 'third party'

      wx.showLoading({
         title: '正在提交数据...',
      })

      co.getShareApi('biz/Bizmg/saveAuthInfo', param, function(res) {
         console.log('saveAuth==', res)
         wx.hideLoading()
         co.toastAllText(res.msg)
         if (res.code == 0) {
            app.Data.shopInfo.Is_Auth = 1;
            co.navigateBack({}, 1000)
            //_this.addUserMoneyRecord()
         }
      })
   },

   //余额支付
   balancePay: function(e) {
      // wx.showLoading({
      //    title: '正在支付中...',
      // })
      var param = {
         Pay_From_ID: 'Biz_' + 91,
         Pay_To_ID: 'Biz_91',
         Abstract: '支付营业执照第三方认证费',
         Money: 1,
         Pay_Password: app.Data.app.appid + 'bizauthpay'
      }
      //余额支付
      co.getShareApi('money/Record/balancePay', param, function(res) {
         if (res.code == 0) {
            app.Data.shopInfo.Biz_Money = res.data.New_Balance
         } else {
            co.toastAllText(res.msg)
         }
      })
   },


   // //添加用户资金记录(提成)
   // addUserMoneyRecord: function() {
   //    var param = {
   //       User_ID: app.Data.shopInfo.Inviter_User_ID,
   //       Abstract: '邀请' + app.Data.shopInfo.Biz_Account + '的提成',
   //       Payer: 'Biz_91',
   //       Money: 10,
   //       Pay_From: '293_pay',
   //       Pay_To: '293_pay',
   //       Pay_For: 'Biz_' + app.Data.shopInfo.Biz_ID
   //    }
   //    co.addMoneyRecord_User(param, function(res){
   //       console.log('res===', res)
   //       if (res.code == 0) {
   //          _this.getUserInfo()
   //       } else {
   //          wx.hideLoading()
   //          co.toastAllText(res.msg)
   //       }
   //    })
   // },

   // //获取用户信息(介绍人的信息(邀请人))
   // getUserInfo: function() {
   //    co.getShareApi('user/Mg/getUserInfo', {
   //       User_ID: app.Data.shopInfo.Inviter_User_ID,
   //       field:'Wx_Openid'
   //    }, function(res){
   //       wx.hideLoading()
   //       co.toastAllText(res.msg)
   //       if (res.code == 0) {
   //          _this.sendWeChatMessage(res.data.Wx_Openid.wxa087ce51079f066e)
   //          co.navigateBack({}, 1000)
   //       }
   //    })
   // },

   // //推送微信模板消息(公众号)
   // sendWeChatMessage: function (openID) {
   //    //被邀请人的shopInfo
   //    var shopInfo = app.Data.shopInfo
   //    //被邀请人的userInfo
   //    var userInfo = app.Data.userInfo
   //    co.getShareApi('api/Weixin/sendTemplateMsg', {
   //       appid: 'wxa087ce51079f066e',
   //       secret: '0c3c47175610bf54df25837e359e5eb6',
   //       openid: openID,
   //       template_id: 'GJNO5bl1RKZgPmFF8Dep3O24vjG9dE0v8545B2NnHqc',
   //       title_value: '你邀请商家入驻【爱共享·码上点】，获得一份的第一笔提成。\n点击详情->登录->我的293零钱->提现(可秒到微信钱包)',
   //       title_color: '#ff0000',
   //       k1_value: userInfo.User_NickName,
   //       k1_color: '#1296db',
   //       k2_value: shopInfo.Biz_Account,
   //       k2_color: '#1296db',
   //       k3_value: 10 + '元',
   //       k3_color: '#1296db',
   //       k4_value: '60元 + 长期分红',
   //       k4_color: '#ff0000',
   //       k5_value: co.getFormatDate(2),
   //       k5_color: '#1296db',
   //       remark_value: '(1)邀请提成10元；\n(2)商家启用提成50元；\n(3)商家长期续用分红。',
   //       remark_color: '#ff0000',
   //       miniprogram:{
   //          appid: "wxd352f5d5fc71beaa",
   //          pagepath: "/03myCenter/01myCenter/myCenter"
   //       }
   //    }, function(res){
   //       console.log('res====', res)
   //       co.toastAllText(res.msg)
   //    })
   // },

})